/**
 * Created by wangxin on 2017-9-26 10:02:01.
 */

'use strict';
const page = {
    url: {
        loadEventRule: contextData.contextPath + '/work/loadEventRule',
        getCoupon: contextData.contextPath + '/work/getCoupon',
    },
};

page.init = function () {
    page.initElement();
    page.initEvent();
};

page.initElement = function () {
    const thisPage = this;
    Dolphin.ajax({
        url: thisPage.url.loadEventRule,
        type: "get",
        forceUrl: 'true',
        data: {eventId: contextData.data.eventId},
        onSuccess: function (data) {
            var createOrgCode = contextData.userData && contextData.userData.createOrgCode || null;
            var store = data && data.store || null;
            if(!(!createOrgCode || !store || (store && !store.length) || store.find(o=>o.code === createOrgCode))){
                var bg = $("<div style='position:fixed;top: 0;bottom:0;left:0;right:0;text-align: center;padding-top:90%;font-weight:700;color: white;font-size:24px;background: rgba(55,55,55,0.75)'>您不能参加本次活动" +
                    "<br />请咨询相关门店人员</div>");
                $("body").append(bg);
            }
            thisPage.renderCouponList(data.rule, $('#couponList'));
        },
        onError: function (e) {
            alert('网络出错啦，请您重新加载');
        }
    });
};

page.initEvent = function () {
};

page.formatterDate = function (val, formatter) {
    if(val){
        var date = Dolphin.string2date(val, "yyyy-MM-ddThh:mm:ss.");
        date.setHours(date.getHours() + 8);
        return Dolphin.date2string(date, formatter || "yyyy-MM-dd");
    }else{
        return '';
    }
};

page.renderCouponList = function (data, panel) {
    var thisPage = this;
    data.forEach(function(d){
        thisPage.renderCoupon(d).appendTo(panel);
    });
};
page.renderCoupon = function (data) {
    var thisPage = this;
    var couponCard = $('<div class="card">');

    var couponInfo = $('<div class="info">').appendTo(couponCard);
    var couponPrice = $('<div class="price">').appendTo(couponInfo);
    if(data.giftPrice > 1){
        couponPrice.append('￥');
        $('<span>').html(data.giftPrice).appendTo(couponPrice);
    }else{
        $('<span>').html(data.giftPrice * 10).appendTo(couponPrice);
        couponPrice.append('折');
    }

    var couponName = $('<div class="name">').appendTo(couponInfo);
    $('<div class="couponName">').html(data.rule).appendTo(couponName);
    $('<div class="couponInfo">').html(data.giftName).appendTo(couponName);

    var couponButton = $('<div class="button">').appendTo(couponInfo);
    $('<button>').html('立即领取').click(function(){
        Dolphin.ajax({
            url:thisPage.url.getCoupon,
            async: false,
            data:{
                prizeId:data._id,
                gameType:'getCoupon',
            },
            success:function(reData){
                $.alert(reData.message);
            }
        });
    }).appendTo(couponButton);

    $('<hr/>').appendTo(couponCard);

    $('<div class="remark">').html(data.giftRemark).appendTo(couponCard);

    return couponCard;
};


$(function () {
    page.init();
});




